package features.advance.edu.algorithm;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author LIN
 * @date 2021-07-30
 */
public class FileReader {
    private static String OS = System.getProperty("os.name").toLowerCase();


    public static void main(String[] args) {
        System.out.println(Arrays.toString(args));
//        if(args == null || args.length == 0 || args[0] == null){
//            return;
//        }
        long fileLength = 3527884404L;
        String fileName = "D:\\AllRepository\\combine\\maven_resporty.zip";
        File file = new File(fileName);
        long alreadyDowndFilelength = file.length();
        System.out.println(alreadyDowndFilelength);
        System.out.println(fileLength-alreadyDowndFilelength);
        combineFile("temp");
//        try(FileInputStream fileInputStream = new FileInputStream(new File("D:\\AllRepository\\combine\\temp232"));
//            FileOutputStream outputStream = new FileOutputStream(new File("D:\\AllRepository\\combine\\tmp232"));
//            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
//            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);) {
//            byte[] data = new byte[1769472];
//            int len = -1;
//            int count = 0;
//            while ((len = bufferedInputStream.read(data))!=-1) {
//                if(count++ < 1){
//                    continue;
//                }
//                bufferedOutputStream.write(data,0,len);
//            }
//            bufferedOutputStream.flush();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }finally {
//
//        }
//        long f = alreadyDowndFilelength / (1024 * 1024 * 10)+1;
//        System.out.println(f);

    }
    public static File skipFile(String fileName,Long skipLength){
        if(fileName == null || fileName.isEmpty()){
            return null;
        }
        File file = new File(fileName);
        FileOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try{
            byte[] data = new byte[1024*1024*10];
            if(skipLength != null){

            }
            outputStream = new FileOutputStream(file,true);
            bufferedOutputStream = new BufferedOutputStream(outputStream);

        }catch (Exception e){

        }finally {
            try {
                if(bufferedInputStream != null){
                    bufferedInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                if(fileInputStream != null){
                    fileInputStream.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            try {
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void combineFile(String fileName){
        FileOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try{
            List<String> fileNameList = new ArrayList<>();
//            String fileName = "算法导论中文版.doc";
            File file = new File("D:\\AllRepository\\combine\\maven_resporty.zip");
            for (int i = 232; i <= 336; i++) {
                fileNameList.add(fileName+i);
            }
            byte[] data = new byte[1024*1024*10];
            outputStream = new FileOutputStream(file,true);
            bufferedOutputStream = new BufferedOutputStream(outputStream);

            for (int i = 0; i < fileNameList.size(); i++) {
                File file1 = new File("D:\\AllRepository\\combine\\"+fileNameList.get(i));
                fileInputStream = new FileInputStream(file1);
                bufferedInputStream = new BufferedInputStream(fileInputStream);

                int len = -1;
                while ((len = bufferedInputStream.read(data))!=-1){
                    bufferedOutputStream.write(data,0,len);
                }
                bufferedOutputStream.flush();
                try {
                    if (bufferedInputStream != null) {
                        bufferedInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public static void splitPDF(String fileName,Long skipLength){
        FileOutputStream outputStream = null;
        FileInputStream fileInputStream = null;
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;
        try{
            String sep = OS.indexOf("linux")!=-1?"/":"\\";
            File file = new File("."+sep+fileName);
            fileInputStream = new FileInputStream(file);
            String charsetName = "utf-8";

            bufferedInputStream = new BufferedInputStream(fileInputStream);
            int len = -1;
            byte[] data = new byte[1024*1024*10];
            StringBuilder sb = new StringBuilder();
            int i = 0;
            while ((len = bufferedInputStream.read(data))!=-1){
                File file1 = new File("."+sep+"temp"+ i++);
                outputStream= new FileOutputStream(file1);
                bufferedOutputStream = new BufferedOutputStream(outputStream);
                bufferedOutputStream.write(data,0,len);
                try {
                    bufferedOutputStream.flush();
                    if (bufferedOutputStream != null) {
                        bufferedOutputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
//           bufferedOutputStream.flush();
//            System.out.println(sb);


        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if (bufferedInputStream != null) {
                    bufferedInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (bufferedOutputStream != null) {
                    bufferedOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileInputStream != null) {
                    fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }


}
